#include "syntaxhighlighter.h"

SyntaxHighlighter::SyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {}

void SyntaxHighlighter::highlightBlock(const QString &text) {
    QString text1 = text;




/**----------------------select update delete---------------------insert**/
    //定义关键字
    QStringList keywords;
    keywords << "select" << "SELECT"<<"update"<<"UPDATE"<<"delete"<<"DELETE"<<"insert"<<"INSERT";

    //设置关键字颜色
    QTextCharFormat keywordFormat;
    keywordFormat.setForeground(QColor(	30,144,255,255));
    //定义正则表达式
    QString keywordPattern = "\\b(" + keywords.join("|") + ")\\b";
    QRegExp keywordRegExp(keywordPattern);

    //匹配所有的关键字
    int index = keywordRegExp.indexIn(text);
    while (index >= 0) {
        int length = keywordRegExp.matchedLength();
        setFormat(index, length, keywordFormat);
        index = keywordRegExp.indexIn(text, index + length);
    }



/**------------------create drop alter-----------------**/
    //定义关键字
    QStringList keywords1;
    keywords1 << "create" << "CREATE"<< "DROP" << "drop" << "alter" << "ALTER" << "MODIFY" << "modify";

    //设置关键字颜色
    QTextCharFormat keywordFormat1;
    keywordFormat1.setForeground(QColor(188,30,81,255));
    //定义正则表达式
    QString keywordPattern1 = "\\b(" + keywords1.join("|") + ")\\b";
    QRegExp keywordRegExp1(keywordPattern1);

    //匹配所有的关键字
    int index1 = keywordRegExp1.indexIn(text1);
    while (index1 >= 0) {
        int length1 = keywordRegExp1.matchedLength();
        setFormat(index1, length1, keywordFormat1);
        index1 = keywordRegExp1.indexIn(text1, index1 + length1);
    }



/**------------------ table-----------------**/
    //定义关键字
    QStringList keywords2;
    keywords2 << "TABLE" << "table";

    //设置关键字颜色
    QTextCharFormat keywordFormat2;
    keywordFormat2.setForeground(QColor(254,184,19,255));
    //定义正则表达式
    QString keywordPattern2 = "\\b(" + keywords2.join("|") + ")\\b";
    QRegExp keywordRegExp2(keywordPattern2);

    //匹配所有的关键字
    int index2 = keywordRegExp2.indexIn(text1);
    while (index2 >= 0) {
        int length1 = keywordRegExp2.matchedLength();
        setFormat(index2, length1, keywordFormat2);
        index2 = keywordRegExp2.indexIn(text1, index2 + length1);
    }


/**--------------------------primary key unique not null----------------------------**/
    //定义关键字
    QStringList keywords3;
    keywords3 << "primary key" << "PRIMARY KEY" << "UNIQUE" << "unique" << "not null" << "NOT NULL" << "check" << "CHECK";

    //设置关键字颜色
    QTextCharFormat keywordFormat3;
    keywordFormat3.setForeground(QColor(220,20,60,255));
    //定义正则表达式
    QString keywordPattern3 = "\\b(" + keywords3.join("|") + ")\\b";
    QRegExp keywordRegExp3(keywordPattern3);

    //匹配所有的关键字
    int index3 = keywordRegExp3.indexIn(text1);
    while (index3 >= 0) {
        int length1 = keywordRegExp3.matchedLength();
        setFormat(index3, length1, keywordFormat3);
        index3 = keywordRegExp3.indexIn(text1, index3 + length1);
    }

/**-----------------------------values from ------------------------------------**/
    //定义关键字
    QStringList keywords4;
    keywords4 << "values" << "VALUES" << "from" << "FROM";

    //设置关键字颜色
    QTextCharFormat keywordFormat4;
    keywordFormat4.setForeground(QColor(60,179,113,255));
    //定义正则表达式
    QString keywordPattern4 = "\\b(" + keywords4.join("|") + ")\\b";
    QRegExp keywordRegExp4(keywordPattern4);

    //匹配所有的关键字
    int index4 = keywordRegExp4.indexIn(text1);
    while (index4 >= 0) {
        int length1 = keywordRegExp4.matchedLength();
        setFormat(index4, length1, keywordFormat4);
        index4 = keywordRegExp4.indexIn(text1, index4 + length1);
    }


/**---------------------------between in where ------------------------------------**/
    //定义关键字
    QStringList keywords5;
    keywords5 << "between" << "into" << "where" << "BETWEEN" << "INTO" << "WHERE";

    //设置关键字颜色
    QTextCharFormat keywordFormat5;
    keywordFormat5.setForeground(QColor(244,164,96,255));
    //定义正则表达式
    QString keywordPattern5 = "\\b(" + keywords5.join("|") + ")\\b";
    QRegExp keywordRegExp5(keywordPattern5);

    //匹配所有的关键字
    int index5 = keywordRegExp5.indexIn(text1);
    while (index5 >= 0) {
        int length1 = keywordRegExp5.matchedLength();
        setFormat(index5, length1, keywordFormat5);
        index5 = keywordRegExp5.indexIn(text1, index5 + length1);
    }


    /**---------------------------between in where ------------------------------------**/
        //定义关键字
        QStringList keywords6;
        keywords6 << "like" << "LIKE" << "SET" << "set";

        //设置关键字颜色
        QTextCharFormat keywordFormat6;
        keywordFormat6.setForeground(QColor(153,50,204));
        //定义正则表达式
        QString keywordPattern6 = "\\b(" + keywords6.join("|") + ")\\b";
        QRegExp keywordRegExp6(keywordPattern6);

        //匹配所有的关键字
        int index6 = keywordRegExp6.indexIn(text1);
        while (index6 >= 0) {
            int length1 = keywordRegExp6.matchedLength();
            setFormat(index6, length1, keywordFormat6);
            index6 = keywordRegExp6.indexIn(text1, index6 + length1);
        }


}
